import Vue from "vue";
import Vuex from 'vuex'

Vue.use(Vuex)

import actions from '@/store/actions'

export default new Vuex.Store({
  state: {
    publishData: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],//发现页面展示的数据
    selectedData: [],//选择照片发布旅游圈数据
    activeFriendData: {},//存放正在聊天的好友信息列表
    latChatRecode: [],//保存聊天记录
    cityInf: {},//保存省份和城市的信息
    scenicInf: {},//保存景点的信息
    // hotelInf:{},//保存酒店信息
    // trafficInf:{},//保存交通信息
    scenicArray: []//保存景点的对象数组
  },
  actions,
  mutations: {
    //修改发现页面展示的数据
    HANDLER_PUBLISH_Data (state, data) {
      state.publishData.push(data)
    },
    //选择照片发布旅游圈数据
    HANDLER_SELECTED_DATA (state, data) {
      console.log('state.selectedData', state.selectedData);

      state.selectedData.push(data.src)
    },
    //存放正在聊天的好友信息列表
    HANDLER_ACTIVE_FRIEND_DATA (state, data) {
      state.activeFriendData = data
    },
    //聊天记录保存
    HANDLER_LAST_CHAT_RECODE (state, data) {
      const exist = state.latChatRecode.some(key => {
        return data.ToUserId === key.ToUserId
      })
      if (!exist) {
        state.latChatRecode.push(data)
      } else {
        for (let i = 0; i < state.latChatRecode.length; i++) {
          const item = state.latChatRecode[i]
          if (item.ToUserId === data.ToUserId) {
            state.latChatRecode[i] = data
          }
        }
      }
    },
    // 存放省份城市信息
    saveCityInf (state, data) {
      let index = data.cityName.indexOf('(')
      if (index !== -1) data.cityName = data.cityName.slice(0, index)
      state.cityInf[data.cityName] = data
    },

    // 存放景点信息
    saveScenicInf (state, data) {
      state.scenicInf[data.addressName] = data
    },


    // 存放酒店信息
    // saveHotelInf (state, data) {
      // const lastScenicObj =  state.scenicArray.pop();
      // lastScenicObj = {...data,...lastScenicObj}
      // state.scenicArray.push(lastScenicObj)
    // },

    // 存放交通信息
    // saveTrafficInf (state, data) {
    //   state.trafficInf = data
    // },

    // 存放景点数组
    saveScenicArray (state, data) {
      if(!!data?.scenicId) state.scenicArray.push({})
      state.scenicArray[state.scenicArray.length - 1] = { ...data, ...state.scenicArray[state.scenicArray.length - 1] };
    }
  }
});